//
//  LaunchViewController.m
//  FZAH
//
//  Created by Yoga on 2017/4/21.
//  Copyright © 2017年 zdqk. All rights reserved.
//

#import "LaunchViewController.h"

@interface LaunchViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *imageArr;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIButton *button;

@end

@implementation LaunchViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        _imageArr = @[@"1b-640x960",@"2b-640x960",@"3b-640x960"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (ScreenHeight == 480) {
        self.imageArr = @[@"1b-640x960",@"2b-640x960",@"3b-640x960"];
    } else if (ScreenHeight == 568) {
        self.imageArr = @[@"1b-640x1136",@"2b-640x1136",@"3b-640x1136"];
    } else if (ScreenHeight == 667) {
        self.imageArr = @[@"1b-750x1334",@"2b-750x1334",@"3b-750x1334"];
    } else if (ScreenHeight == 736) {
        self.imageArr = @[@"1b-1242x2208",@"2b-1242x2208",@"3b-1242x2208"];
    }
    
    
    [self.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.button.frame = self.view.frame;
//    self.button.layer.cornerRadius = self.button.height * 0.5;
    
    
    [self.scrollView setContentSize:CGSizeMake(ScreenWidth * self.imageArr.count, ScreenHeight)];
    for (NSInteger i = 0; i < self.imageArr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.imageArr objectAtIndex:i]]];
        imageView.frame = CGRectMake(ScreenWidth * i, 0, ScreenWidth, ScreenHeight);
        if (i == self.imageArr.count - 1) {
            imageView.userInteractionEnabled = YES;
            [imageView addSubview:self.button];
        }
        [self.scrollView addSubview:imageView];
    }
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.scrollView.frame = self.view.bounds;
    self.pageControl.frame = CGRectMake(0, ScreenHeight-30, ScreenWidth, 20);
}

- (void)buttonClick:(UIButton *)sender {
    if (self.finish) {
        self.finish();
    }
}

//UIScrollViewDelegate方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sView
{
    NSInteger index = fabs(sView.contentOffset.x) / sView.frame.size.width;
    //NSLog(@"%d",index);
    [self.pageControl setCurrentPage:index];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}
- (UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] init];
        [_pageControl setBackgroundColor:[UIColor clearColor]];
        _pageControl.currentPage = 0;
        _pageControl.numberOfPages = _imageArr.count;
        
        _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    }
    return _pageControl;
}
- (UIButton *)button {
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_button setTitle:@"完成" forState:UIControlStateNormal];
//        [_button setBackgroundColor:[UIColor newBlueColor]];
    }
    return _button;
}

@end
